<template>
  <div class="call-center-apply">
    <div class="container">
      <div class="left-menu">
        <div class="menu-title">{{ $t('callcenter.callcenterSetting') }}</div>
        <nav class="menu-list">
          <div class="icon-box">
            <i class="iconfont icon-setting"></i>
          </div>
          <span>{{ $t('callcenter.callcenterSetting') }}</span>
        </nav>
      </div>
      <div class="main">
        <div class="apply-box">
          <p>{{isAduiting ? $t('callcenter.apply.text1') : $t('callcenter.apply.text2') }}</p>
          <div class="tip">
            <img :src="image1">
            <div class="content">
              <p>{{$t('callcenter.callcenter')}}</p>
              <span>{{ $t('callcenter.apply.text3')  }}</span>
            </div>
            <div class="divider"></div>
            <div class="help">
              <p>{{ $t('callcenter.apply.text4') }}</p>
              <ul>
                <li><a href=''>{{ $t('callcenter.apply.helpWiki') }}</a></li>
              </ul>
            </div>
          </div>
          <el-button :type="isAduiting ? 'default' : 'primary'" @click="applyDialogVisible = true" :disabled="isAduiting">{{ $t('callcenter.apply.applyToOpen') }}</el-button>
        </div>

      </div>
    </div>
    <!-- 申请开通的对话框 -->
    <el-dialog :title="$t('callcenter.apply.applyOpenCallcenter')" :visible.sync="applyDialogVisible" width="30%" @close="applyDialogClosed">
      <el-form :model="applyForm" :rules="applyFormRules" ref="applyFormRef" label-position="top">
        <el-form-item :label="$t('common.label.companyName')" prop="companyName">
          <el-input v-model="applyForm.companyName" :placeholder="$t('common.placeholder.inputCompanyName2')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('common.label.contact')" prop="linkmanName">
          <el-input v-model="applyForm.linkmanName" :placeholder="$t('common.placeholder.inputContact')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('common.label.contactTel')" prop="phone">
          <el-input v-model="applyForm.phone" :placeholder="$t('common.placeholder.inputContactPhone2')"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="applyDialogVisible = false">{{ $t('common.button.cancel') }}</el-button>
        <el-button type="primary" :disabled="pending" :loading="pending" @click="apply">{{ $t('callcenter.apply.apply') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as CallCenterApi from '@src/api/CallCenterApi'
import { validatePhone } from '@src/util/validator'
import { getOssUrl } from '@src/util/assets'
const image1 = getOssUrl('/screen-data-time.png')
export default {
  name: 'call-center-apply',
  data() {
    // 验证固话手机号的规则
    const checkMobile = (rule, value, cb) => {
      if (validatePhone(value, 3)) {
        return cb()
      }
      cb(new Error(this.$t('common.placeholder.inputValidateNumber')))
    }
    return {
      image1,
      pending: false,
      isAduiting: false,
      applyDialogVisible: false,
      applyForm: {
        companyName: '',
        linkmanName: '',
        phone: ''
      },
      // 添加表单的验证规则对象
      applyFormRules: {
        companyName: [
          { required: true, message: this.$t('common.placeholder.inputCompanyName2'), trigger: 'blur' },
        ],
        linkmanName: [
          { required: true, message: this.$t('common.placeholder.inputContact'), trigger: 'blur' },
        ],
        phone: [
          { required: true, message: this.$t('common.placeholder.inputPhoneNumber'), trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    applyDialogClosed() {
      this.$refs.applyFormRef.resetFields()
    },
    apply() {
      this.$refs.applyFormRef.validate(async valid => {
        if (!valid) return
        try {
          this.pending = true;
          const { code, message } = await CallCenterApi.saveAccount(this.applyForm)
          if (code !== 0) return this.$platform.notification({
            title: this.$t('callcenter.apply.applyFailed'),
            message: message || '',
            type: 'error',
          })
          this.pending = false;
          this.applyDialogVisible = false
          this.applyDialogClosed()    
          this.isAduiting = true
          this.$platform.notification({
            title: this.$t('callcenter.apply.applySuccess'),
            type: 'success',
          })
        } catch (error) {
          this.pending = false;
          console.error(error)
        }   
      })
    }
  }
}
</script>
<style lang="scss">
.call-center-apply {
  padding: 10px;
  position: relative;
  box-sizing: border-box;
  min-height: 100vh;
  max-width: 100vw;
  .container {
    display: flex;
    align-items: flex-start;
    .left-menu {
      width: 25%;
      min-width: 200px;
      background: #fff;
      box-sizing: border-box;
      border-radius: 3px;
      overflow: hidden;
      position: sticky;
      position: -webkit-sticky;
      top: 10px;
      height: 100vh;
      .menu-title {
        font-size: 18px;
        color: #454648;
        padding: 10px;
        font-weight: 600;
      }
      .menu-list {
        border-left: 3px solid transparent;
        border-top: 1px solid #f4f4f4;
        padding: 10px 15px;
        position: relative;
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          background: rgb(246, 246, 246);
        }
        span {
          font-size: 12px;
        }
        .left-border {
          height: 100%;
          width: 3px;
          position: absolute;
          left: 0;
          background: #55b7b4;
          top: 0;
        }
        .icon-box {
          width: 22px;
          height: 22px;
          display: flex;
          align-items: center;
          margin-right: 13px;
          i {
            font-size: 16px;
            font-weight: 600;
          }
        }
      }
    }
    .main {
      margin-left: 10px;
      flex: 1;
      min-width: 920px;
      box-sizing: border-box;
      min-height: 100vh;

      .apply-box {
        background: #fff;
        display: flex;
        flex-direction: column;
        padding: 40px 84px 60px 84px;
        margin: 0 auto;
        p {
          height: 32px;
          font-size: 24px;
          font-weight: 500;
          color: #051a13;
          text-align: center;
          margin-bottom: 24px;
        }
        .tip{
          height:180px;
          background:#F7F8F9;
          border-radius:4px;
          margin-bottom: 50px;
          display: flex;
          padding: 30px;
          img {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            margin-right: 10px;
          }
          .content {
            flex: 1;
            width: 330px;
            p {
              font-size: 16px;
              text-align: left;
              margin-bottom: 8px;
            }
          }
          .divider {
            margin: 0 20px;
            width: 2px;
            height: 100%;
            background-color: #eee;
          }
          .help {
            flex: 1; 
            p {
              font-size: 16px;
              text-align: left;
              margin-left: 1em;
              margin-bottom: 8px;
            }
            a {
              color: #409EFF;
            }
          }
        }
        button {
          width: 96px;
          margin: 0 auto;
        }
      }

      .operation-bar-container {
        display: flex;
        justify-content: flex-end;
        background: #fff;
        padding: 10px;
      }
    }
  }
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.el-table th > .cell {
  font-size: 14px !important;
  color: #333 !important;
  font-weight: 500 !important;
}

::-webkit-scrollbar-track {
  background: transparent;
}
</style>